package com.gbb.simple.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.app.ActivityOptionsCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.gbb.common.utils.AppManager;
import com.gbb.common.utils.KeyBoardUtils;
import com.gbb.common.utils.ToastUtil;

/**
 * 继承时请务必先调用：
 * <pre>
 * 	protected void onCreate(Bundle savedInstanceState) {
 * 		super.onCreate(savedInstanceState);//子类中该行代码一定不能少
 *      //
 * }
 * <pre>
 */
public abstract class BaseFragmentActivity extends AppCompatActivity {

    protected Activity mContext;

    protected ProgressDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        AppManager.getInstance().addActivity(this);
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        initStatusBar();
    }

    protected void initStatusBar() {
        
    }

    public void startActivity(Class<? extends Activity> targetClass) {
        startActivity(new Intent(mContext,targetClass));
    }

    public void startActivity(Intent intent,int enterResId,int exitResId) {
        Bundle animBundle = ActivityOptionsCompat.makeCustomAnimation(mContext,enterResId,exitResId).toBundle();
        ActivityCompat.startActivity(mContext,intent,animBundle);
    }

    public void startActivity(Class<? extends Activity> targetClass,int enterResId,int exitResId) {
        Bundle animBundle = ActivityOptionsCompat.makeCustomAnimation(mContext,enterResId,exitResId).toBundle();
        ActivityCompat.startActivity(mContext,new Intent(mContext,targetClass),animBundle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if(KeyBoardUtils.isShouldHideInput(v,ev)) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                if(imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(),0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        if(getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    //--------------------------------------Dialog、Toast-------------------------------------------


    protected void showLoadingDialog(String message) {
        if(loadingDialog == null) {
            loadingDialog = new ProgressDialog(mContext);
            loadingDialog.setCancelable(false);
            loadingDialog.setCanceledOnTouchOutside(false);
        }
        loadingDialog.setMessage(TextUtils.isEmpty(message) ? "正在加载..." : message);
        loadingDialog.show();
    }

    protected void closeLoadingDialog() {
        if(loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    public ProgressDialog getLoadingDialog() {
        return loadingDialog;
    }

    public void showToast(String toastContent) {
        ToastUtil.showToast(toastContent);
    }

    public void showToast(int textId) {
        ToastUtil.showToast(textId);
    }

    //----------------------------------------Get Resource------------------------------------------

    protected Spanned getHtml(int stringRes) {
        return Html.fromHtml(getString(stringRes));
    }

    protected Spanned getHtml(int stringRes,Object... formatArgs) {
        return Html.fromHtml(getString(stringRes,formatArgs));
    }

    @Override
    protected void onDestroy() {
        AppManager.getInstance().removeActivity(this);
        super.onDestroy();
    }

}
